//
//  JSMDreamCitySpecialDetailsViewController.m
//  TakeYouTourTogether
//
//  Created by king on 15/12/1.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMDreamCitySpecialDetailsViewController.h"
#import "JSMDreamCitySpecialResultModel.h"
#import "JSMDreamCitySpecialDetailModel.h"
#import "JSMDreamCitySpecialDetailTableViewCell.h"
#import "JSMDreamCityDetailsRouteViewController.h"
#import "ShareView.h"
@interface JSMDreamCitySpecialDetailsViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) JSMDreamCitySpecialResultModel *dreamCitySpecialResultModel;
@property(nonatomic, strong) BaseTableView *myTableView;
@property(nonatomic, strong) NSArray *tempArr;
@property(nonatomic, strong) UIImage *myImage;
@property(nonatomic, strong) BaseButton *detailsButton;
@property(nonatomic, strong) BaseButton *collectButton;
@property(nonatomic, strong) BaseButton *shareButton;
/**
 *  分享标题
 */
@property (nonatomic, strong) NSString *shareTitle;

/**
 *  分享内容
 */
@property (nonatomic, strong) NSString *shareText;

/**
 *  分享链接地址
 */
@property (nonatomic, strong) NSString *shareUrl;
@property(nonatomic, strong) ShareView *shareView;

@end

@implementation JSMDreamCitySpecialDetailsViewController

#pragma mark - Method viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    self.myTableView.bounces = NO;
    [self.view addSubview:self.myTableView];
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self requestDataFromNetWorking];
}
#pragma mark - Method 每个分区返回一行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
#pragma mark - Method 返回N个分区(网络请求数据个数)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.tempArr.count;
}
#pragma mark - Method 专区cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *resue = @"dreamCitySpecialDetailTableViewResue";
    JSMDreamCitySpecialDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resue];
    if (!cell) {
        cell = [[JSMDreamCitySpecialDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resue];
    }
    JSMDreamCitySpecialDetailModel *temp = self.tempArr[indexPath.section];
    cell.model = temp;
    return cell;
}
#pragma mark - Method 设置每个分区的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 根据图片的不同尺寸, 返回不同的高度
    // 1. 先根据图片名找图片的UIImage
    JSMDreamCitySpecialDetailModel *temp = self.tempArr[indexPath.section];
//        self.myImage = [UIImage imageNamed:temp.imageUrl];
    // 根据图片原有尺寸计算比例, 然后根据目标宽度, 计算出返回的高度
//        CGFloat picHeight = self.view.frame.size.width *self.myImage.size.height / self.myImage.size.width;
    // 要注意字体大小保持一致
    // 文字的大小作为一个描述信息放到字典中成为一对key-value
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
    // 根据文本字体大小来计算文本的cell高度
    // 第一个参数: 文字显示的最大尺寸范围(375, 0)
    CGRect rect = [temp.imageDesc boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-10, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return 333*ALL_EDITION_HEIGHT + rect.size.height +20;
}
#pragma mark - Method 设置分区头视图的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30*ALL_EDITION_HEIGHT;
}
#pragma mark - Method 创建分区的头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *backView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30*ALL_EDITION_HEIGHT)];
    backView.backgroundColor = [UIColor grayColor];
    BaseLabel *titleLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30*ALL_EDITION_HEIGHT)];
    titleLabel.textColor = [UIColor whiteColor];
    [backView addSubview:titleLabel];
    titleLabel.text = [self.tempArr[section] imageTitle];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    return backView;
}
#pragma mark - Method requestDataFromNetWorking 网络请求数据
- (void)requestDataFromNetWorking {
    NSString *strURL = [NSString stringWithFormat:@"http://124.232.137.88:9655/lyapp/custom/Distribute/newLineDetail.do?lineId=%@", self.tempModel.vid];
    NSLog(@"%@", strURL);
    [AFNetworkingTool getUrl:strURL hudSuperView:self.view result:JSON success:^(id result) {
        NSDictionary *dic = result;
        self.dreamCitySpecialResultModel = [[JSMDreamCitySpecialResultModel alloc] init];
        [self.dreamCitySpecialResultModel setValuesForKeysWithDictionary:dic];
        self.tempArr = self.dreamCitySpecialResultModel.listImageArr;
        NSLog(@"%@", self.tempArr);
        // 懒加载 路线详情button
        [self.detailsButton setImage:[UIImage imageNamed:ROUTE_IMAGE] forState:UIControlStateNormal];
        // 懒加载 收藏button
        self.collectButton.backgroundColor = [UIColor blackColor];
        // 懒加载 分享按钮
        [self.shareButton setImage:[UIImage imageNamed:SHARE_IMAGE] forState:UIControlStateNormal];
        // tableView 刷新
        [self.myTableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark - Method 创建旅游路线按钮
- (BaseButton *)detailsButton {
    if (!_detailsButton) {
    _detailsButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_detailsButton];
    _detailsButton.backgroundColor = [UIColor blackColor];
    _detailsButton.alpha = 0.8;
    _detailsButton.layer.cornerRadius = 30*ALL_EDITION_WIDTH/2;
    [_detailsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(99*ALL_EDITION_HEIGHT);
        make.right.equalTo(self.view.mas_right).offset(-19*ALL_EDITION_WIDTH);
        make.width.mas_equalTo(30*ALL_EDITION_WIDTH);
        make.height.mas_equalTo(30*ALL_EDITION_WIDTH);
    }];
    _detailsButton.layer.borderWidth = 2;
    _detailsButton.layer.borderColor = [[UIColor whiteColor] CGColor];
    [_detailsButton addTarget:self action:@selector(detailsButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _detailsButton;
}
#pragma mark - Method 收藏按钮
- (BaseButton *)collectButton {
    if (!_collectButton) {
        _collectButton = [BaseButton buttonWithType:UIButtonTypeCustom];
        _collectButton.backgroundColor = [UIColor blackColor];
        _collectButton.alpha = 0.8;
        _collectButton.layer.borderWidth = 2;
        _collectButton.layer.borderColor = [[UIColor whiteColor] CGColor];
        _collectButton.layer.cornerRadius = 30*ALL_EDITION_WIDTH/2;
        [_collectButton addTarget:self action:@selector(collectMessageButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_collectButton];
        [_collectButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top).offset(140*ALL_EDITION_HEIGHT);
            make.right.equalTo(self.view.mas_right).offset(-19*ALL_EDITION_WIDTH);
            make.width.mas_equalTo(30*ALL_EDITION_WIDTH);
            make.height.mas_equalTo(30*ALL_EDITION_WIDTH);
        }];
        if ([SaveTool isHaveModelInPlist:self.tempModel type:SaveDreamCitySpecialModel]) {
            [_collectButton setImage:[UIImage imageNamed:SAVE_NOTHAVE_IMAGE] forState:UIControlStateNormal];
        } else {
            [_collectButton setImage:[UIImage imageNamed:SAVE_ISHAVE_IMAGE] forState:UIControlStateNormal];
        }
    }
    return _collectButton;
}
#pragma mark - Method BarButton右按钮集合
- (BaseButton *)shareButton {
    if (!_shareButton) {
        // BarButtonRight -> 分享
        _shareButton = [BaseButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:_shareButton];
        _shareButton.backgroundColor = [UIColor blackColor];
        _shareButton.alpha = 0.8;
        _shareButton.layer.cornerRadius = 30*ALL_EDITION_WIDTH/2;
        [_shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top).offset(180*ALL_EDITION_HEIGHT);
            make.right.equalTo(self.view.mas_right).offset(-19*ALL_EDITION_WIDTH);
            make.width.mas_equalTo(30*ALL_EDITION_WIDTH);
            make.height.mas_equalTo(30*ALL_EDITION_WIDTH);
        }];
        _shareButton.layer.borderWidth = 2;
        _shareButton.layer.borderColor = [[UIColor whiteColor] CGColor];
        [_shareButton addTarget:self action:@selector(shareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _shareButton;
}
#pragma mark 分享
- (void)shareButtonClick:(BaseButton *)button {
    if (!self.shareView) {
        self.shareView = [[ShareView alloc] initWithFrame:self.view.bounds];
        self.shareView.alpha = 0.0;
        [self.view addSubview:self.shareView];
        self.shareView.shareTitle = [NSString stringWithFormat:@"%@", self.tempModel.name];
        self.shareView.shareText = NSLocalizedString(@"分享内容", nil);
        self.shareView.shareUrl = [NSString stringWithFormat:@"%@", self.tempModel.imageUrl];
        [UIView animateWithDuration:1 animations:^{
            self.shareView.alpha = 1.0;
        }];
    } else {
        [UIView animateWithDuration:1 animations:^{
            self.shareView.alpha = 1.0;

        }];
    }
    
}

//- (void)shareToWeiboBase
//{
//    
//    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
//    authRequest.redirectURI = @"http://www.sina.com";
//    authRequest.scope = @"all";
//    
//    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare] authInfo:authRequest access_token:nil];
//    
//    request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
//                         @"Other_Info_1": [NSNumber numberWithInt:123],
//                         @"Other_Info_2": @[@"obj1", @"obj2"],
//                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
//    
//    [WeiboSDK sendRequest:request];
//    
//}
//- (WBMessageObject *)messageToShare
//{
//    
//    WBMessageObject *message = [WBMessageObject message];
//    BOOL hadInstalledWeibo = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weibo://"]];
//    
//    if(hadInstalledWeibo){
//        WBWebpageObject *webpage = [WBWebpageObject object];
//        webpage.objectID = @"identifier1";
//        webpage.title = self.shareTitle;
//        webpage.description = self.shareText;
//        //  可改为自定义图片
//        webpage.thumbnailData = UIImageJPEGRepresentation([UIImage imageNamed:@"logo.jpg"], 0.5);
//        webpage.webpageUrl = self.shareUrl;
//        
//        message.mediaObject = webpage;
//    }
//    
//    message.text = [NSString stringWithFormat:@"%@ - %@ - %@", self.shareTitle, self.shareText, self.shareUrl];
//    
//    
//    return message;
//    
//}
//+ (instancetype)sharedInstance
//{
//    
//    static JSMDreamCitySpecialDetailsViewController* util;
//    static dispatch_once_t onceToken;
//    
//    dispatch_once(&onceToken, ^{
//        
//        util = [[JSMDreamCitySpecialDetailsViewController alloc] init];
//        
//    });
//    return util;
//    
//}
//
//- (instancetype)init
//{
//    
//    static id obj=nil;
//    static dispatch_once_t onceToken;
//    
//    dispatch_once(&onceToken, ^{
//        
//        obj = [super init];
//        if (obj) {
//            
//        }
//        
//    });
//    self=obj;
//    return self;
//    
//}

#pragma mark 收藏
- (void)collectMessageButton:(BaseButton *)button{
    NSString *message;
    if ([button.imageView.image isEqual:[UIImage imageNamed:SAVE_ISHAVE_IMAGE]]) {
        [SaveTool cancelSaveModelInPlist:self.tempModel type:SaveDreamCitySpecialModel];
        [button setImage:[UIImage imageNamed:SAVE_NOTHAVE_IMAGE] forState:UIControlStateNormal];
        message = @"取消收藏";
    }else{
        [SaveTool saveModelToPlist:self.tempModel type:SaveDreamCitySpecialModel];
        [button setImage:[UIImage imageNamed:SAVE_ISHAVE_IMAGE] forState:UIControlStateNormal];
        message = @"已收藏";
    }
    UIAlertController *alertControler = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alertControler animated:YES completion:^{
    }];
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
#pragma mark - Method 跳转到旅游路线页面
- (void)detailsButtonAction:(UIButton *)detailsButton {
    JSMDreamCityDetailsRouteViewController *routeDetailsVC = [[JSMDreamCityDetailsRouteViewController alloc] init];
    routeDetailsVC.hidesBottomBarWhenPushed = YES;
    // 传值
    routeDetailsVC.dreamCitySpecialTravelArray = self.dreamCitySpecialResultModel.listDayTravelArr;
    [self.navigationController pushViewController:routeDetailsVC animated:YES];
}
#pragma mark - Method didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
